<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">

<head>
    <!-- 页面meta -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>广告管理</title>
    <!-- Tell the browser to be responsive to screen width -->
    <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
    <link rel="stylesheet" href="/plugins/bootstrap/css/bootstrap.min.css">
    <link rel="stylesheet" href="/plugins/adminLTE/css/AdminLTE.css">
    <link rel="stylesheet" href="/plugins/adminLTE/css/skins/_all-skins.min.css">
    <link rel="stylesheet" href="/css/style.css">
	<script src="/plugins/jQuery/jquery-2.2.3.min.js"></script>
    <script src="/plugins/bootstrap/js/bootstrap.min.js"></script>

	<script src="/media/layui/layui.js"></script>

	<script type="text/javascript">
        //跳转页面
        function goPage(pageIndex) {
            location.href="/content_page/"+pageIndex+"/5";
        }


        //异步绑定新的下拉框
        $(function () {
            $.post("/contentCategory_list",function (result) {
                //alert(JSON.stringify(result));
				for(var i=0;i<result.length;i++){
                    $("#addSelect").append("<option value='"+result[i].id+"'>"+result[i].name+"</option>");
				}
            },"json");
        });

        //新增的时候文件上传
        function uploadImg_add(){
            var formData = new FormData();//异步模拟表单
            formData.append("file", document.getElementById("file_add").files[0]);  //向表单中添加一个文件上传框
            $.ajax({
                url:"/uploadImg", //请求地址
                type:"post",      //文件上传必须post
                data: formData,
                /**
                 *必须false才会自动加上正确的Content-Type
                 */
                contentType: false,
                /**
                 * 必须false才会避开jQuery对 formdata 的默认处理
                 * XMLHttpRequest会对 formdata 进行正确的处理
                 */
                processData: false,
                success:function(result){
                    alert(JSON.stringify(result));
                    layui.use('table', function() {
                        if(result.code==200){
                            layer.msg("上传成功", {icon : 6},function(){
                                $("#upload_img_add").prop("src",result.message); //成功之后给src重新赋值  显示成功之后的图片地址
                                $("#upload_img_add_pic").val(result.message); //隐藏值.新增使用！
                            });
                        }else if(result.code==777){
							layer.msg(result.message, {icon : 5},function(){});
						}
                        else{
                            layer.msg("上传失败", {icon : 5},function(){});
                        }
                    });
                }
            });
        }
        
        //异步新增
		function addContent() {
            $.ajax({
                url:"/content_add",
                type:"post",
                dataType:"json",
                data:$("#addForm").serialize(),
                success:function(result){
                    alert(JSON.stringify(result));
                    layui.use('table', function() {
                        if(result.code=="601"){
                            layer.msg("新增成功", {icon : 6},function(){
                                window.location.reload();//刷新页面
                            });
                        }else{
                            layer.msg("新增失败", {icon : 5},function(){
                                window.location.reload();//刷新页面
                            });
                        }
                    });
                }
            });
        }


	</script>
    
</head>

<body class="hold-transition skin-red sidebar-mini">
  <!-- .box-body -->
                
                    <div class="box-header with-border">
                        <h3 class="box-title">广告管理</h3>
                    </div>

                    <div class="box-body">

                        <!-- 数据表格 -->
                        <div class="table-box">

                            <!--工具栏-->
                            <div class="pull-left">
                                <div class="form-group form-inline">
                                    <div class="btn-group">
                                        <button type="button" class="btn btn-default" title="新建" data-toggle="modal" data-target="#addModal" ><i class="fa fa-file-o"></i> 新建</button>
                                        <button type="button" class="btn btn-default" title="删除" ><i class="fa fa-trash-o"></i> 删除</button>
                                        <button type="button" class="btn btn-default" title="开启" onclick='confirm("你确认要开启吗？")'><i class="fa fa-check"></i> 开启</button>
                                        <button type="button" class="btn btn-default" title="屏蔽" onclick='confirm("你确认要屏蔽吗？")'><i class="fa fa-ban"></i> 屏蔽</button>
                                        <button type="button" class="btn btn-default" title="刷新" onclick="window.location.reload();"><i class="fa fa-refresh"></i> 刷新</button>
                                    </div>
                                </div>
                            </div>
                            <div class="box-tools pull-right">
                                <div class="has-feedback">
							                                  
                                </div>
                            </div>
                            <!--工具栏/-->

			                  <!--数据列表-->
			                  <table id="dataList" class="table table-bordered table-striped table-hover dataTable">
			                      <thead>
			                          <tr>
			                              <th class="" style="padding-right:0px">
			                                  <input id="selall" type="checkbox" class="icheckbox_square-blue">
			                              </th> 
										  <th class="sorting_asc">广告ID</th>
									      <th class="sorting">分类ID</th>
									      <th class="sorting">标题</th>
									      <th class="sorting">URL</th>		
									      <th class="sorting">图片</th>	
									      <th class="sorting">排序</th>		
									      <th class="sorting">是否有效</th>											     						      							
					                      <th class="text-center">操作</th>
			                          </tr>
			                      </thead>
			                      <tbody>
			                          <tr th:each="content:${pageUtils.records}">
			                              <td><input  type="checkbox"></td>			                              
				                          <td th:text="${content.id}">1</td>
									      <td th:text="${content.categoryId}">1</td>
									      <td th:text="${content.title}">促销海报1</td>
									      <td th:text="${content.url}">http://wwww.hb.com/hd1.html</td>
									      <td>
									      	<img alt="" src="" th:src="${content.pic}" width="100px" height="50px">
									      </td>
									      <td th:text="${content.sortOrder}">1</td>
									      <td th:text="${content.status}==0?'有效':'无效'">有效</td>
		                                  <td class="text-center">                                           
		                                 	  <button type="button" class="btn bg-olive btn-xs" data-toggle="modal" data-target="#editModal">修改</button>
											  <button type="button" class="btn bg-red btn-xs" data-toggle="modal" th:onclick="'deleteById();'">删除</button>
										  </td>
			                          </tr>
			                      </tbody>
			                  </table>
			                  <!--数据列表/-->
							<!-- 分页组件 -->
							<div class="row">
								<div class="col-sm-6"><div class="dataTables_paginate paging_bootstrap" id="sample-table_paginate">
									<ul class="pagination">
										<li class="prev"><a href="#" th:onclick="'goPage('+${pageUtils.pageIndex==1?1:pageUtils.pageIndex-1}+')'">上一页</a></li>
										<li th:each="num:${pageUtils.numbers}" th:class="${pageUtils.pageIndex==num}? active"><a href="#" th:onclick="'goPage('+${num}+')'" th:text="${num}">1</a></li>
										<li class="next"><a href="#" th:onclick="'goPage('+${pageUtils.pageIndex==pageUtils.pageCount?pageUtils.pageIndex:pageUtils.pageIndex+1}+')'">下一页</a></li>
										<li class="next"><a href="#" th:text="|当前${pageUtils.pageIndex}/${pageUtils.pageCount}页|">XXXX</a></li>
										<li class="next"><a href="#" th:text="|共${pageUtils.totalCount}条|">XXXX</a></li>
									</ul>
								</div>
								</div>
							</div>
							<!-- 分页组件 -->
                        </div>
                        <!-- 数据表格 /-->
                     </div>
                    <!-- /.box-body -->



  <!-- 新增窗口 -->
  <form id="addForm">
  <div class="modal fade" id="addModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
	  <div class="modal-dialog" >
		  <div class="modal-content">
			  <div class="modal-header">
				  <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
				  <h3 id="myModalLabel">广告新增</h3>
			  </div>
			  <div class="modal-body">

				  <table class="table table-bordered table-striped"  width="800px">
					  <tr>
						  <td>广告分类</td>
						  <td>
							  <select class="form-control" name="categoryId" id="addSelect">
							  </select>
						  </td>
					  </tr>
					  <tr>
						  <td>标题</td>
						  <td><input  class="form-control" name="title" placeholder="标题" >  </td>
					  </tr>
					  <tr>
						  <td>URL</td>
						  <td><input  class="form-control" name="url" placeholder="URL" >  </td>
					  </tr>
					  <tr>
						  <td>排序</td>
						  <td><input  class="form-control" name="sortOrder" placeholder="排序" >  </td>
					  </tr>
					  <tr>
						  <td>广告图片</td>
						  <td>
							  <table>
								  <tr>
									  <td>
										  <!-- 文件上传的框 -->
										  <input type="file" id="file_add" />

										  <!-- 上传成功之后地址藏起来  提交到数据库 -->
										  <input type="hidden" name="pic" id="upload_img_add_pic"/>

										  <button class="btn btn-primary" type="button" onclick="uploadImg_add();">
											  上传
										  </button>
									  </td>
									  <td>
										  <!-- 显示的图片 -->
										  <img src="#" id="upload_img_add" width="150px" height="100px">
									  </td>
								  </tr>
							  </table>
						  </td>
					  </tr>
					  <tr>
						  <td>是否有效</td>
						  <td>
							  <input type="checkbox" name="status" class="icheckbox_square-blue" value="0">
						  </td>
					  </tr>
				  </table>

			  </div>
			  <div class="modal-footer">
				  <button class="btn btn-success" data-dismiss="modal" aria-hidden="true" onclick="addContent();">新增</button>
				  <button class="btn btn-default" data-dismiss="modal" aria-hidden="true">关闭</button>
			  </div>
		  </div>
	  </div>
  </div>
  </form>
		
<!-- 编辑窗口 -->
<div class="modal fade" id="editModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  <div class="modal-dialog" >
	<div class="modal-content">
		<div class="modal-header">
			<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
			<h3 id="myModalLabel">广告编辑</h3>
		</div>
		<div class="modal-body">							
			
			<table class="table table-bordered table-striped"  width="800px">
				<tr>
		      		<td>广告分类</td>
		      		<td>
		      			<select class="form-control" >
		                </select>
		      		</td>
		      	</tr>
		      	<tr>
		      		<td>标题</td>
		      		<td><input  class="form-control" placeholder="标题" >  </td>
		      	</tr>
			    <tr>
		      		<td>URL</td>
		      		<td><input  class="form-control" placeholder="URL" >  </td>
		      	</tr>	
		      	<tr>
		      		<td>排序</td>
		      		<td><input  class="form-control" placeholder="排序" >  </td>
		      	</tr>			      	
		      	<tr>
		      		<td>广告图片</td>
		      		<td>
						<table>
							<tr>
								<td>
								<input type="file" id="file" />				                
					                <button class="btn btn-primary" type="button" >
				                   		上传
					                </button>	
					            </td>
								<td>
									<img  src="{{entity.pic}}" width="200px" height="100px">
								</td>
							</tr>						
						</table>
		      		</td>
		      	</tr>	      
		      	<tr>
		      		<td>是否有效</td>
		      		<td>
		      		   <input type="checkbox" class="icheckbox_square-blue" >
		      		</td>
		      	</tr>  	
			 </table>				
			
		</div>
		<div class="modal-footer">						
			<button class="btn btn-success" data-dismiss="modal" aria-hidden="true">保存</button>
			<button class="btn btn-default" data-dismiss="modal" aria-hidden="true">关闭</button>
		</div>
	  </div>
	</div>
</div>

</body>

</html>